﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Linq;

namespace TfsPolicyPack.CustomRegexPathPolicy.Controls
{
    public partial class EditRegexListControl : UserControl
    {
        #region Events

        public event EventHandler Changed;

        #endregion

        #region Constructor

        public EditRegexListControl()
        {
            InitializeComponent();
        }

        #endregion

        #region Public Methods

        public void Init(IEnumerable<string> patterns)
        {
            if (patterns != null)
            {
                foreach (string pattern in patterns)
                {
                    RegexListBox.Items.Add(pattern);
                }
            }

            RefreshUserInterface();
        }

        public bool HasPatterns()
        {
            return RegexListBox.Items.Count > 0;
        }

        public List<string> GetPatterns()
        {
            return RegexListBox.Items.OfType<string>().ToList();
        }

        #endregion

        #region Non Public Methods

        protected void OnChange()
        {
            if (Changed != null)
                Changed(this, EventArgs.Empty);
        }

        private void RefreshUserInterface()
        {
            AddButton.Enabled = RegexTextBox.Text.Length > 0;
            RemoveButton.Enabled = RegexListBox.SelectedIndex != -1;
            CopyButton.Enabled = RegexListBox.SelectedIndex != -1;
            CopyAllButton.Enabled = RegexListBox.Items.Count > 0;
        }

        private void AddButton_Click(object sender, EventArgs e)
        {
            AddPattern();
        }

        private void RegexTextBox_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                AddPattern();
        }

        private void AddPattern()
        {
            string pattern = RegexTextBox.Text;

            try
            {
                Regex.IsMatch(string.Empty, pattern, RegexOptions.Singleline);
            }
            catch (ArgumentException argumentException)
            {
                MessageBox.Show("The provided regex is invalid: " + argumentException.Message);
                return;
            }
            catch (Exception exception)
            {
                MessageBox.Show(string.Format("An unknown problem occurred:\n\n{0}", exception));
                return;
            }

            RegexListBox.Items.Add(pattern);
            RegexTextBox.Clear();
            RefreshUserInterface();
            OnChange();
        }

        private void RemoveButton_Click(object sender, EventArgs e)
        {
            if (RegexListBox.SelectedIndex != -1)
            {
                RegexListBox.Items.RemoveAt(RegexListBox.SelectedIndex);
                RefreshUserInterface();
                OnChange();
            }
        }

        private void CopyButton_Click(object sender, EventArgs e)
        {
            if (RegexListBox.SelectedIndex != -1)
                Clipboard.SetText(Convert.ToString(RegexListBox.Items[RegexListBox.SelectedIndex]), TextDataFormat.Text);
        }

        private void CopyAllButton_Click(object sender, EventArgs e)
        {
            if (RegexListBox.Items.Count == 0)
                return;

            StringBuilder result = new StringBuilder();

            foreach (string pattern in RegexListBox.Items)
            {
                result.AppendLine(pattern);
            }

            Clipboard.SetText(result.ToString(), TextDataFormat.Text);
        }

        private void RegexTextBox_TextChanged(object sender, EventArgs e)
        {
            RefreshUserInterface();
        }

        private void RegexListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            RefreshUserInterface();
        }

        #endregion
    }
}